package cn.insect.servlet;

import cn.insect.pojo.Insect;
import cn.insect.service.InsectService;
import cn.insect.service.impl.InsectServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @auth 吴阳基
 * @date 2024/1/3 16:17
 */
@WebServlet(urlPatterns = "/select")
public class Select extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf-8");

        String name = req.getParameter("name");
        int cid = 0;
        if (req.getParameter("cid") != null) {
            cid = Integer.parseInt(req.getParameter("cid"));
        }

        InsectService insectService = new InsectServiceImpl();
        List<Insect> list = insectService.getSelect(name, cid);

        req.setAttribute("list", list);
        req.getRequestDispatcher("select.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
